In [1]:
import glob
import math
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
import random
import sklearn.metrics as metrics

from tensorflow.keras import optimizers
from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger, LearningRateScheduler
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import add, concatenate, Conv2D, Dense, Dropout, Flatten, Input
from tensorflow.keras.layers import Activation, AveragePooling2D, BatchNormalization, MaxPooling2D
from tensorflow.keras.regularizers import l2
from tensorflow.keras.utils import to_categorical


%matplotlib inline
In [2]:
                            # Set up 'ggplot' style
plt.style.use('ggplot')     # if want to use the default style, set 'classic'
plt.rcParams['ytick.right']     = True
plt.rcParams['ytick.labelright']= True
plt.rcParams['ytick.left']      = False
plt.rcParams['ytick.labelleft'] = False
plt.rcParams['font.family']     = 'Arial'
In [3]:
# where am i?
%pwd
Out[3]:
'C:\\Users\\david\\Documents\\ImageNet'
In [4]:
flowers = glob.glob('./data/flr_*.jpg')
fungus = glob.glob('./data/fgs_*.jpg')
rocks = glob.glob('./data/rck_*.jpg')

pixel_flowers = glob.glob('./data/pxl_flower_*.jpeg')
pixel_umbrella = glob.glob('./data/pxl_umbrella_*.jpeg')
print("There are %s, %s flower, %s fungus, %s rock and %s umbrella pictures" %(len(flowers), len(pixel_flowers), len(fungus), len(rocks), len(pixel_umbrella)))
There are 1269, 1792 flower, 856 fungus, 1007 rock and 420 umbrella pictures
In [5]:
# Randomly show 10 examples of the images
from IPython.display import Image
    
dataset = flowers #flowers #fungus #rocks

for i in range(0, 5):
    index = random.randint(0, len(dataset)-1)   
    print("Showing:", dataset[index])
    
    img = mpimg.imread(dataset[index])
    imgplot = plt.imshow(img)
    plt.show()

#Image(dataset[index])
Showing: ./data\flr_01521.jpg
Showing: ./data\flr_00739.jpg
Showing: ./data\flr_00328.jpg
Showing: ./data\flr_00681.jpg
Showing: ./data\flr_01522.jpg

Extract the training and testing datasets

In [6]:
# Load the data
trDatOrg       = np.load('flrnonflr-train-imgs96-0.8.npz')['arr_0']
trLblOrg       = np.load('flrnonflr-train-labels96-0.8.npz')['arr_0']
tsDatOrg       = np.load('flrnonflr-test-imgs96-0.8.npz')['arr_0']
tsLblOrg       = np.load('flrnonflr-test-labels96-0.8.npz')['arr_0']
In [7]:
print("For the training and test datasets:")
print("The shapes are %s, %s, %s, %s" \
      %(trDatOrg.shape, trLblOrg.shape, tsDatOrg.shape, tsLblOrg.shape))
For the training and test datasets:
The shapes are (4264, 96, 96, 3), (4264,), (1067, 96, 96, 3), (1067,)
In [8]:
# Randomly show 10 examples of the images

data = tsDatOrg
label = tsLblOrg

for i in range(20):
    index = random.randint(0, len(data)-1)
    print("Showing %s index image, It is %s" %(index, label[index]))
    imgplot = plt.imshow(data[index])
    plt.show()
Showing 487 index image, It is 1.0
Showing 963 index image, It is 0.0
Showing 643 index image, It is 0.0
Showing 156 index image, It is 1.0
Showing 111 index image, It is 1.0
Showing 425 index image, It is 1.0
Showing 994 index image, It is 0.0
Showing 275 index image, It is 1.0
Showing 67 index image, It is 1.0
Showing 579 index image, It is 1.0
Showing 1023 index image, It is 0.0
Showing 404 index image, It is 1.0
Showing 89 index image, It is 1.0
Showing 439 index image, It is 1.0
Showing 509 index image, It is 1.0
Showing 176 index image, It is 1.0
Showing 276 index image, It is 1.0
Showing 744 index image, It is 0.0
Showing 117 index image, It is 1.0
Showing 75 index image, It is 1.0
In [9]:
# Convert the data into 'float32'
# Rescale the values from 0~255 to 0~1
trDat       = trDatOrg.astype('float32')/255
tsDat       = tsDatOrg.astype('float32')/255

# Retrieve the row size of each image
# Retrieve the column size of each image
imgrows     = trDat.shape[1]
imgclms     = trDat.shape[2]
channel     = 3

# # reshape the data to be [samples][width][height][channel]
# # This is required by Keras framework
# trDat       = trDat.reshape(trDat.shape[0], imgrows, imgclms, channel)
# tsDat       = tsDat.reshape(tsDat.shape[0], imgrows, imgclms, channel)

# Perform one hot encoding on the labels
# Retrieve the number of classes in this problem
trLbl       = to_categorical(trLblOrg)
tsLbl       = to_categorical(tsLblOrg)
num_classes = tsLbl.shape[1]
In [10]:
# fix random seed for reproducibility
seed = 29
np.random.seed(seed)


modelname = 'FlowerPower'

def createBaselineModel():
    inputs = Input(shape=(imgrows, imgclms, channel))
    x = Conv2D(30, (4, 4), activation='relu')(inputs)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Conv2D(50, (4, 4), activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Dropout(0.3)(x)
    x = Flatten()(x)
    x = Dense(32, activation='relu')(x)
    x = Dense(num_classes, activation='softmax')(x)
    
    model = Model(inputs=[inputs],outputs=x)
    
    model.compile(loss='categorical_crossentropy', 
                  optimizer='adam',
                  metrics=['accuracy'])
    return model

optmz       = optimizers.Adam(lr=0.001)

def resLyr(inputs,
           numFilters=16,
           kernelSz=3,
           strides=1,
           activation='relu',
           batchNorm=True,
           convFirst=True,
           lyrName=None):
    convLyr = Conv2D(numFilters, kernel_size=kernelSz, strides=strides, 
                     padding='same', kernel_initializer='he_normal', 
                     kernel_regularizer=l2(1e-4), 
                     name=lyrName+'_conv' if lyrName else None)
    x = inputs
    if convFirst:
        x = convLyr(x)
        if batchNorm:
            x = BatchNormalization(name=lyrName+'_bn' if lyrName else None)(x)
        if activation is not None:
            x = Activation(activation,name=lyrName+'_'+activation if lyrName else None)(x)
    else:
        if batchNorm:
            x = BatchNormalization(name=lyrName+'_bn' if lyrName else None)(x)
        if activation is not None:
            x = Activation(activation, name=lyrName+'_'+activation if lyrName else None)(x)
        x = convLyr(x)
    return x


def resBlkV1(inputs,
             numFilters=16,
             numBlocks=3,
             downsampleOnFirst=True,
             names=None):
    x = inputs
    for run in range(0,numBlocks):
        strides = 1
        blkStr = str(run+1)
        if downsampleOnFirst and run == 0:
            strides = 2
        y = resLyr(inputs=x, numFilters=numFilters, strides=strides,
                   lyrName=names+'_Blk'+blkStr+'_Res1' if names else None)
        y = resLyr(inputs=y, numFilters=numFilters, activation=None,
                   lyrName=names+'_Blk'+blkStr+'_Res2' if names else None)
        if downsampleOnFirst and run == 0:
            x = resLyr(inputs=x, numFilters=numFilters, kernelSz=1,
                       strides=strides, activation=None, batchNorm=False,
                       lyrName=names+'_Blk'+blkStr+'_lin' if names else None)
        x = add([x,y], name=names+'_Blk'+blkStr+'_add' if names else None)
        x = Activation('relu', name=names+'_Blk'+blkStr+'_relu' if names else None)(x)
    return x

def createResNetV1(inputShape=(imgrows, imgclms, channel),
                   numClasses=2):
    inputs = Input(shape=inputShape)
    v = resLyr(inputs, lyrName='Inpt')
    v = resBlkV1(inputs=v, numFilters=16, numBlocks=3,
                 downsampleOnFirst=False, names='Stg1')
    v = Dropout(0.30)(v)
    v = resBlkV1(inputs=v, numFilters=32, numBlocks=3,
                 downsampleOnFirst=True, names='Stg2')
    v = Dropout(0.40)(v)
    v = resBlkV1(inputs=v, numFilters=64, numBlocks=3,
                 downsampleOnFirst=True, names='Stg3')
    v = Dropout(0.50)(v)
    v = resBlkV1(inputs=v, numFilters=128, numBlocks=3,
                 downsampleOnFirst=True, names='Stg4')
    v = Dropout(0.50)(v)
    v = resBlkV1(inputs=v, numFilters=128, numBlocks=3,
                 downsampleOnFirst=False, names='Stg5')
    v = Dropout(0.50)(v)
    v = AveragePooling2D(pool_size=8, name='AvgPool')(v)
    v = Flatten()(v) 
    outputs = Dense(numClasses, activation='softmax', 
                    kernel_initializer='he_normal')(v)
    model = Model(inputs=inputs,outputs=outputs)
    model.compile(loss='categorical_crossentropy', optimizer=optmz, 
                  metrics=['accuracy'])
    return model



# Setup the models
model       = createResNetV1() # This is meant for training
modelGo     = createResNetV1() # This is used for final testing

model.summary()
WARNING:tensorflow:From D:\DocumentsDDrive\Installed_Files\Anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\keras\initializers.py:104: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with distribution=normal is deprecated and will be removed in a future version.
Instructions for updating:
`normal` is a deprecated alias for `truncated_normal`
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 96, 96, 3)    0                                            
__________________________________________________________________________________________________
Inpt_conv (Conv2D)              (None, 96, 96, 16)   448         input_1[0][0]                    
__________________________________________________________________________________________________
Inpt_bn (BatchNormalization)    (None, 96, 96, 16)   64          Inpt_conv[0][0]                  
__________________________________________________________________________________________________
Inpt_relu (Activation)          (None, 96, 96, 16)   0           Inpt_bn[0][0]                    
__________________________________________________________________________________________________
Stg1_Blk1_Res1_conv (Conv2D)    (None, 96, 96, 16)   2320        Inpt_relu[0][0]                  
__________________________________________________________________________________________________
Stg1_Blk1_Res1_bn (BatchNormali (None, 96, 96, 16)   64          Stg1_Blk1_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg1_Blk1_Res1_relu (Activation (None, 96, 96, 16)   0           Stg1_Blk1_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg1_Blk1_Res2_conv (Conv2D)    (None, 96, 96, 16)   2320        Stg1_Blk1_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg1_Blk1_Res2_bn (BatchNormali (None, 96, 96, 16)   64          Stg1_Blk1_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg1_Blk1_add (Add)             (None, 96, 96, 16)   0           Inpt_relu[0][0]                  
                                                                 Stg1_Blk1_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg1_Blk1_relu (Activation)     (None, 96, 96, 16)   0           Stg1_Blk1_add[0][0]              
__________________________________________________________________________________________________
Stg1_Blk2_Res1_conv (Conv2D)    (None, 96, 96, 16)   2320        Stg1_Blk1_relu[0][0]             
__________________________________________________________________________________________________
Stg1_Blk2_Res1_bn (BatchNormali (None, 96, 96, 16)   64          Stg1_Blk2_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg1_Blk2_Res1_relu (Activation (None, 96, 96, 16)   0           Stg1_Blk2_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg1_Blk2_Res2_conv (Conv2D)    (None, 96, 96, 16)   2320        Stg1_Blk2_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg1_Blk2_Res2_bn (BatchNormali (None, 96, 96, 16)   64          Stg1_Blk2_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg1_Blk2_add (Add)             (None, 96, 96, 16)   0           Stg1_Blk1_relu[0][0]             
                                                                 Stg1_Blk2_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg1_Blk2_relu (Activation)     (None, 96, 96, 16)   0           Stg1_Blk2_add[0][0]              
__________________________________________________________________________________________________
Stg1_Blk3_Res1_conv (Conv2D)    (None, 96, 96, 16)   2320        Stg1_Blk2_relu[0][0]             
__________________________________________________________________________________________________
Stg1_Blk3_Res1_bn (BatchNormali (None, 96, 96, 16)   64          Stg1_Blk3_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg1_Blk3_Res1_relu (Activation (None, 96, 96, 16)   0           Stg1_Blk3_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg1_Blk3_Res2_conv (Conv2D)    (None, 96, 96, 16)   2320        Stg1_Blk3_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg1_Blk3_Res2_bn (BatchNormali (None, 96, 96, 16)   64          Stg1_Blk3_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg1_Blk3_add (Add)             (None, 96, 96, 16)   0           Stg1_Blk2_relu[0][0]             
                                                                 Stg1_Blk3_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg1_Blk3_relu (Activation)     (None, 96, 96, 16)   0           Stg1_Blk3_add[0][0]              
__________________________________________________________________________________________________
dropout (Dropout)               (None, 96, 96, 16)   0           Stg1_Blk3_relu[0][0]             
__________________________________________________________________________________________________
Stg2_Blk1_Res1_conv (Conv2D)    (None, 48, 48, 32)   4640        dropout[0][0]                    
__________________________________________________________________________________________________
Stg2_Blk1_Res1_bn (BatchNormali (None, 48, 48, 32)   128         Stg2_Blk1_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg2_Blk1_Res1_relu (Activation (None, 48, 48, 32)   0           Stg2_Blk1_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg2_Blk1_Res2_conv (Conv2D)    (None, 48, 48, 32)   9248        Stg2_Blk1_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg2_Blk1_lin_conv (Conv2D)     (None, 48, 48, 32)   544         dropout[0][0]                    
__________________________________________________________________________________________________
Stg2_Blk1_Res2_bn (BatchNormali (None, 48, 48, 32)   128         Stg2_Blk1_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg2_Blk1_add (Add)             (None, 48, 48, 32)   0           Stg2_Blk1_lin_conv[0][0]         
                                                                 Stg2_Blk1_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg2_Blk1_relu (Activation)     (None, 48, 48, 32)   0           Stg2_Blk1_add[0][0]              
__________________________________________________________________________________________________
Stg2_Blk2_Res1_conv (Conv2D)    (None, 48, 48, 32)   9248        Stg2_Blk1_relu[0][0]             
__________________________________________________________________________________________________
Stg2_Blk2_Res1_bn (BatchNormali (None, 48, 48, 32)   128         Stg2_Blk2_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg2_Blk2_Res1_relu (Activation (None, 48, 48, 32)   0           Stg2_Blk2_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg2_Blk2_Res2_conv (Conv2D)    (None, 48, 48, 32)   9248        Stg2_Blk2_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg2_Blk2_Res2_bn (BatchNormali (None, 48, 48, 32)   128         Stg2_Blk2_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg2_Blk2_add (Add)             (None, 48, 48, 32)   0           Stg2_Blk1_relu[0][0]             
                                                                 Stg2_Blk2_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg2_Blk2_relu (Activation)     (None, 48, 48, 32)   0           Stg2_Blk2_add[0][0]              
__________________________________________________________________________________________________
Stg2_Blk3_Res1_conv (Conv2D)    (None, 48, 48, 32)   9248        Stg2_Blk2_relu[0][0]             
__________________________________________________________________________________________________
Stg2_Blk3_Res1_bn (BatchNormali (None, 48, 48, 32)   128         Stg2_Blk3_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg2_Blk3_Res1_relu (Activation (None, 48, 48, 32)   0           Stg2_Blk3_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg2_Blk3_Res2_conv (Conv2D)    (None, 48, 48, 32)   9248        Stg2_Blk3_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg2_Blk3_Res2_bn (BatchNormali (None, 48, 48, 32)   128         Stg2_Blk3_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg2_Blk3_add (Add)             (None, 48, 48, 32)   0           Stg2_Blk2_relu[0][0]             
                                                                 Stg2_Blk3_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg2_Blk3_relu (Activation)     (None, 48, 48, 32)   0           Stg2_Blk3_add[0][0]              
__________________________________________________________________________________________________
dropout_1 (Dropout)             (None, 48, 48, 32)   0           Stg2_Blk3_relu[0][0]             
__________________________________________________________________________________________________
Stg3_Blk1_Res1_conv (Conv2D)    (None, 24, 24, 64)   18496       dropout_1[0][0]                  
__________________________________________________________________________________________________
Stg3_Blk1_Res1_bn (BatchNormali (None, 24, 24, 64)   256         Stg3_Blk1_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg3_Blk1_Res1_relu (Activation (None, 24, 24, 64)   0           Stg3_Blk1_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg3_Blk1_Res2_conv (Conv2D)    (None, 24, 24, 64)   36928       Stg3_Blk1_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg3_Blk1_lin_conv (Conv2D)     (None, 24, 24, 64)   2112        dropout_1[0][0]                  
__________________________________________________________________________________________________
Stg3_Blk1_Res2_bn (BatchNormali (None, 24, 24, 64)   256         Stg3_Blk1_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg3_Blk1_add (Add)             (None, 24, 24, 64)   0           Stg3_Blk1_lin_conv[0][0]         
                                                                 Stg3_Blk1_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg3_Blk1_relu (Activation)     (None, 24, 24, 64)   0           Stg3_Blk1_add[0][0]              
__________________________________________________________________________________________________
Stg3_Blk2_Res1_conv (Conv2D)    (None, 24, 24, 64)   36928       Stg3_Blk1_relu[0][0]             
__________________________________________________________________________________________________
Stg3_Blk2_Res1_bn (BatchNormali (None, 24, 24, 64)   256         Stg3_Blk2_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg3_Blk2_Res1_relu (Activation (None, 24, 24, 64)   0           Stg3_Blk2_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg3_Blk2_Res2_conv (Conv2D)    (None, 24, 24, 64)   36928       Stg3_Blk2_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg3_Blk2_Res2_bn (BatchNormali (None, 24, 24, 64)   256         Stg3_Blk2_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg3_Blk2_add (Add)             (None, 24, 24, 64)   0           Stg3_Blk1_relu[0][0]             
                                                                 Stg3_Blk2_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg3_Blk2_relu (Activation)     (None, 24, 24, 64)   0           Stg3_Blk2_add[0][0]              
__________________________________________________________________________________________________
Stg3_Blk3_Res1_conv (Conv2D)    (None, 24, 24, 64)   36928       Stg3_Blk2_relu[0][0]             
__________________________________________________________________________________________________
Stg3_Blk3_Res1_bn (BatchNormali (None, 24, 24, 64)   256         Stg3_Blk3_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg3_Blk3_Res1_relu (Activation (None, 24, 24, 64)   0           Stg3_Blk3_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg3_Blk3_Res2_conv (Conv2D)    (None, 24, 24, 64)   36928       Stg3_Blk3_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg3_Blk3_Res2_bn (BatchNormali (None, 24, 24, 64)   256         Stg3_Blk3_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg3_Blk3_add (Add)             (None, 24, 24, 64)   0           Stg3_Blk2_relu[0][0]             
                                                                 Stg3_Blk3_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg3_Blk3_relu (Activation)     (None, 24, 24, 64)   0           Stg3_Blk3_add[0][0]              
__________________________________________________________________________________________________
dropout_2 (Dropout)             (None, 24, 24, 64)   0           Stg3_Blk3_relu[0][0]             
__________________________________________________________________________________________________
Stg4_Blk1_Res1_conv (Conv2D)    (None, 12, 12, 128)  73856       dropout_2[0][0]                  
__________________________________________________________________________________________________
Stg4_Blk1_Res1_bn (BatchNormali (None, 12, 12, 128)  512         Stg4_Blk1_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg4_Blk1_Res1_relu (Activation (None, 12, 12, 128)  0           Stg4_Blk1_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg4_Blk1_Res2_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg4_Blk1_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg4_Blk1_lin_conv (Conv2D)     (None, 12, 12, 128)  8320        dropout_2[0][0]                  
__________________________________________________________________________________________________
Stg4_Blk1_Res2_bn (BatchNormali (None, 12, 12, 128)  512         Stg4_Blk1_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg4_Blk1_add (Add)             (None, 12, 12, 128)  0           Stg4_Blk1_lin_conv[0][0]         
                                                                 Stg4_Blk1_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg4_Blk1_relu (Activation)     (None, 12, 12, 128)  0           Stg4_Blk1_add[0][0]              
__________________________________________________________________________________________________
Stg4_Blk2_Res1_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg4_Blk1_relu[0][0]             
__________________________________________________________________________________________________
Stg4_Blk2_Res1_bn (BatchNormali (None, 12, 12, 128)  512         Stg4_Blk2_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg4_Blk2_Res1_relu (Activation (None, 12, 12, 128)  0           Stg4_Blk2_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg4_Blk2_Res2_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg4_Blk2_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg4_Blk2_Res2_bn (BatchNormali (None, 12, 12, 128)  512         Stg4_Blk2_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg4_Blk2_add (Add)             (None, 12, 12, 128)  0           Stg4_Blk1_relu[0][0]             
                                                                 Stg4_Blk2_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg4_Blk2_relu (Activation)     (None, 12, 12, 128)  0           Stg4_Blk2_add[0][0]              
__________________________________________________________________________________________________
Stg4_Blk3_Res1_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg4_Blk2_relu[0][0]             
__________________________________________________________________________________________________
Stg4_Blk3_Res1_bn (BatchNormali (None, 12, 12, 128)  512         Stg4_Blk3_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg4_Blk3_Res1_relu (Activation (None, 12, 12, 128)  0           Stg4_Blk3_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg4_Blk3_Res2_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg4_Blk3_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg4_Blk3_Res2_bn (BatchNormali (None, 12, 12, 128)  512         Stg4_Blk3_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg4_Blk3_add (Add)             (None, 12, 12, 128)  0           Stg4_Blk2_relu[0][0]             
                                                                 Stg4_Blk3_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg4_Blk3_relu (Activation)     (None, 12, 12, 128)  0           Stg4_Blk3_add[0][0]              
__________________________________________________________________________________________________
dropout_3 (Dropout)             (None, 12, 12, 128)  0           Stg4_Blk3_relu[0][0]             
__________________________________________________________________________________________________
Stg5_Blk1_Res1_conv (Conv2D)    (None, 12, 12, 128)  147584      dropout_3[0][0]                  
__________________________________________________________________________________________________
Stg5_Blk1_Res1_bn (BatchNormali (None, 12, 12, 128)  512         Stg5_Blk1_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg5_Blk1_Res1_relu (Activation (None, 12, 12, 128)  0           Stg5_Blk1_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg5_Blk1_Res2_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg5_Blk1_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg5_Blk1_Res2_bn (BatchNormali (None, 12, 12, 128)  512         Stg5_Blk1_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg5_Blk1_add (Add)             (None, 12, 12, 128)  0           dropout_3[0][0]                  
                                                                 Stg5_Blk1_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg5_Blk1_relu (Activation)     (None, 12, 12, 128)  0           Stg5_Blk1_add[0][0]              
__________________________________________________________________________________________________
Stg5_Blk2_Res1_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg5_Blk1_relu[0][0]             
__________________________________________________________________________________________________
Stg5_Blk2_Res1_bn (BatchNormali (None, 12, 12, 128)  512         Stg5_Blk2_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg5_Blk2_Res1_relu (Activation (None, 12, 12, 128)  0           Stg5_Blk2_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg5_Blk2_Res2_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg5_Blk2_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg5_Blk2_Res2_bn (BatchNormali (None, 12, 12, 128)  512         Stg5_Blk2_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg5_Blk2_add (Add)             (None, 12, 12, 128)  0           Stg5_Blk1_relu[0][0]             
                                                                 Stg5_Blk2_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg5_Blk2_relu (Activation)     (None, 12, 12, 128)  0           Stg5_Blk2_add[0][0]              
__________________________________________________________________________________________________
Stg5_Blk3_Res1_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg5_Blk2_relu[0][0]             
__________________________________________________________________________________________________
Stg5_Blk3_Res1_bn (BatchNormali (None, 12, 12, 128)  512         Stg5_Blk3_Res1_conv[0][0]        
__________________________________________________________________________________________________
Stg5_Blk3_Res1_relu (Activation (None, 12, 12, 128)  0           Stg5_Blk3_Res1_bn[0][0]          
__________________________________________________________________________________________________
Stg5_Blk3_Res2_conv (Conv2D)    (None, 12, 12, 128)  147584      Stg5_Blk3_Res1_relu[0][0]        
__________________________________________________________________________________________________
Stg5_Blk3_Res2_bn (BatchNormali (None, 12, 12, 128)  512         Stg5_Blk3_Res2_conv[0][0]        
__________________________________________________________________________________________________
Stg5_Blk3_add (Add)             (None, 12, 12, 128)  0           Stg5_Blk2_relu[0][0]             
                                                                 Stg5_Blk3_Res2_bn[0][0]          
__________________________________________________________________________________________________
Stg5_Blk3_relu (Activation)     (None, 12, 12, 128)  0           Stg5_Blk3_add[0][0]              
__________________________________________________________________________________________________
dropout_4 (Dropout)             (None, 12, 12, 128)  0           Stg5_Blk3_relu[0][0]             
__________________________________________________________________________________________________
AvgPool (AveragePooling2D)      (None, 1, 1, 128)    0           dropout_4[0][0]                  
__________________________________________________________________________________________________
flatten (Flatten)               (None, 128)          0           AvgPool[0][0]                    
__________________________________________________________________________________________________
dense (Dense)                   (None, 2)            258         flatten[0][0]                    
==================================================================================================
Total params: 1,985,794
Trainable params: 1,981,346
Non-trainable params: 4,448
__________________________________________________________________________________________________
In [11]:
# Create checkpoint for the training
# This checkpoint performs model saving when
# an epoch gives highest testing accuracy
# filepath        = modelname + ".hdf5"
# checkpoint      = ModelCheckpoint(filepath, 
#                                   monitor='val_acc', 
#                                   verbose=0, 
#                                   save_best_only=True, 
#                                   mode='max')

#                             # Log the epoch detail into csv
# csv_logger      = CSVLogger(modelname +'.csv')
# callbacks_list  = [checkpoint,csv_logger]

def lrSchedule(epoch):
    lr  = 1e-3
    
    if epoch > 70:
        lr  *= 0.5e-3
        
    elif epoch > 50:
        lr  *= 1e-3
        
    elif epoch > 40:
        lr  *= 1e-2
        
    elif epoch > 30:
        lr  *= 1e-1
        
    print('Learning rate: ', lr)
    
    return lr

LRScheduler     = LearningRateScheduler(lrSchedule)

                            # Create checkpoint for the training
                            # This checkpoint performs model saving when
                            # an epoch gives highest testing accuracy
filepath        = modelname + ".hdf5"
checkpoint      = ModelCheckpoint(filepath, 
                                  monitor='val_acc', 
                                  verbose=0, 
                                  save_best_only=True, 
                                  mode='max')

                            # Log the epoch detail into csv
csv_logger      = CSVLogger(modelname +'.csv')
callbacks_list  = [checkpoint, csv_logger, LRScheduler]
#callbacks_list  = [checkpoint, csv_logger]
In [12]:
# Fit the model
# This is where the training starts
# model.fit(trDat, 
#           trLbl, 
#           validation_data=(tsDat, tsLbl), 
#           epochs=120, 
#           batch_size=32,
#           callbacks=callbacks_list)

datagen = ImageDataGenerator(width_shift_range=0.2,
                             height_shift_range=0.2,
                             rotation_range=45,
                             zoom_range=0.5,
                             #zca_epsilon=1e-6,
                             #zca_whitening=True,
                             fill_mode='nearest',
                             horizontal_flip=True,
                             vertical_flip=False)

model.fit_generator(datagen.flow(trDat, trLbl, batch_size=32),
                    validation_data=(tsDat, tsLbl),
                    epochs=120, 
                    verbose=1,
                    steps_per_epoch=len(trDat)/32,
                    callbacks=callbacks_list)
Learning rate:  0.001
Epoch 1/120
134/133 [==============================] - 60s 445ms/step - loss: 1.3645 - acc: 0.6693 - val_loss: 1.1928 - val_acc: 0.6654
Learning rate:  0.001
Epoch 2/120
134/133 [==============================] - 32s 240ms/step - loss: 1.0203 - acc: 0.7505 - val_loss: 1.1837 - val_acc: 0.6514
Learning rate:  0.001
Epoch 3/120
134/133 [==============================] - 32s 239ms/step - loss: 0.9597 - acc: 0.7729 - val_loss: 0.8982 - val_acc: 0.7854
Learning rate:  0.001
Epoch 4/120
134/133 [==============================] - 32s 236ms/step - loss: 0.8938 - acc: 0.8011 - val_loss: 1.0584 - val_acc: 0.6392
Learning rate:  0.001
Epoch 5/120
134/133 [==============================] - 32s 242ms/step - loss: 0.8541 - acc: 0.8057 - val_loss: 0.8006 - val_acc: 0.8051
Learning rate:  0.001
Epoch 6/120
134/133 [==============================] - 33s 243ms/step - loss: 0.8036 - acc: 0.8130 - val_loss: 0.8145 - val_acc: 0.7732
Learning rate:  0.001
Epoch 7/120
134/133 [==============================] - 32s 238ms/step - loss: 0.7927 - acc: 0.8132 - val_loss: 0.7240 - val_acc: 0.8313
Learning rate:  0.001
Epoch 8/120
134/133 [==============================] - 32s 238ms/step - loss: 0.7469 - acc: 0.8148 - val_loss: 0.7151 - val_acc: 0.8416
Learning rate:  0.001
Epoch 9/120
134/133 [==============================] - 32s 241ms/step - loss: 0.7114 - acc: 0.8186 - val_loss: 0.6457 - val_acc: 0.8435
Learning rate:  0.001
Epoch 10/120
134/133 [==============================] - 32s 238ms/step - loss: 0.6863 - acc: 0.8239 - val_loss: 0.6793 - val_acc: 0.8088
Learning rate:  0.001
Epoch 11/120
134/133 [==============================] - 32s 236ms/step - loss: 0.6451 - acc: 0.8335 - val_loss: 0.5988 - val_acc: 0.8735
Learning rate:  0.001
Epoch 12/120
134/133 [==============================] - 32s 238ms/step - loss: 0.6318 - acc: 0.8372 - val_loss: 0.5438 - val_acc: 0.8847
Learning rate:  0.001
Epoch 13/120
134/133 [==============================] - 31s 233ms/step - loss: 0.5920 - acc: 0.8414 - val_loss: 0.5253 - val_acc: 0.8735
Learning rate:  0.001
Epoch 14/120
134/133 [==============================] - 31s 233ms/step - loss: 0.5861 - acc: 0.8417 - val_loss: 0.5466 - val_acc: 0.8519
Learning rate:  0.001
Epoch 15/120
134/133 [==============================] - 32s 237ms/step - loss: 0.5744 - acc: 0.8395 - val_loss: 0.5299 - val_acc: 0.8566
Learning rate:  0.001
Epoch 16/120
134/133 [==============================] - 32s 236ms/step - loss: 0.5510 - acc: 0.8428 - val_loss: 0.5239 - val_acc: 0.8725
Learning rate:  0.001
Epoch 17/120
134/133 [==============================] - 30s 221ms/step - loss: 0.5282 - acc: 0.8498 - val_loss: 0.5072 - val_acc: 0.8641
Learning rate:  0.001
Epoch 18/120
134/133 [==============================] - 32s 237ms/step - loss: 0.5239 - acc: 0.8438 - val_loss: 0.4416 - val_acc: 0.8875
Learning rate:  0.001
Epoch 19/120
134/133 [==============================] - 32s 238ms/step - loss: 0.5026 - acc: 0.8482 - val_loss: 0.4590 - val_acc: 0.8754
Learning rate:  0.001
Epoch 20/120
134/133 [==============================] - 32s 238ms/step - loss: 0.5057 - acc: 0.8421 - val_loss: 0.4882 - val_acc: 0.8604
Learning rate:  0.001
Epoch 21/120
134/133 [==============================] - 32s 237ms/step - loss: 0.4903 - acc: 0.8470 - val_loss: 0.4502 - val_acc: 0.8735
Learning rate:  0.001
Epoch 22/120
134/133 [==============================] - 32s 237ms/step - loss: 0.4677 - acc: 0.8545 - val_loss: 0.4503 - val_acc: 0.8735
Learning rate:  0.001
Epoch 23/120
134/133 [==============================] - 32s 239ms/step - loss: 0.4474 - acc: 0.8640 - val_loss: 0.4085 - val_acc: 0.8932
Learning rate:  0.001
Epoch 24/120
134/133 [==============================] - 32s 235ms/step - loss: 0.4496 - acc: 0.8671 - val_loss: 0.4143 - val_acc: 0.8754
Learning rate:  0.001
Epoch 25/120
134/133 [==============================] - 31s 234ms/step - loss: 0.4349 - acc: 0.8622 - val_loss: 0.4716 - val_acc: 0.8388
Learning rate:  0.001
Epoch 26/120
134/133 [==============================] - 31s 234ms/step - loss: 0.4429 - acc: 0.8620 - val_loss: 0.4158 - val_acc: 0.8763
Learning rate:  0.001
Epoch 27/120
134/133 [==============================] - 32s 238ms/step - loss: 0.4231 - acc: 0.8647 - val_loss: 0.3808 - val_acc: 0.8885
Learning rate:  0.001
Epoch 28/120
134/133 [==============================] - 33s 246ms/step - loss: 0.4042 - acc: 0.8752 - val_loss: 0.3602 - val_acc: 0.8997
Learning rate:  0.001
Epoch 29/120
134/133 [==============================] - 31s 234ms/step - loss: 0.4095 - acc: 0.8694 - val_loss: 0.3933 - val_acc: 0.8716
Learning rate:  0.001
Epoch 30/120
134/133 [==============================] - 32s 240ms/step - loss: 0.4093 - acc: 0.8685 - val_loss: 0.3664 - val_acc: 0.8932
Learning rate:  0.001
Epoch 31/120
134/133 [==============================] - 32s 241ms/step - loss: 0.4076 - acc: 0.8720 - val_loss: 0.4798 - val_acc: 0.8407
Learning rate:  0.0001
Epoch 32/120
134/133 [==============================] - 31s 234ms/step - loss: 0.3713 - acc: 0.8822 - val_loss: 0.3524 - val_acc: 0.8969
Learning rate:  0.0001
Epoch 33/120
134/133 [==============================] - 32s 236ms/step - loss: 0.3565 - acc: 0.8871 - val_loss: 0.3437 - val_acc: 0.9035
Learning rate:  0.0001
Epoch 34/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3441 - acc: 0.8972 - val_loss: 0.3529 - val_acc: 0.8988
Learning rate:  0.0001
Epoch 35/120
134/133 [==============================] - 32s 235ms/step - loss: 0.3523 - acc: 0.8864 - val_loss: 0.3328 - val_acc: 0.9053
Learning rate:  0.0001
Epoch 36/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3486 - acc: 0.8941 - val_loss: 0.3369 - val_acc: 0.9035
Learning rate:  0.0001
Epoch 37/120
134/133 [==============================] - 32s 235ms/step - loss: 0.3446 - acc: 0.8911 - val_loss: 0.3265 - val_acc: 0.9072
Learning rate:  0.0001
Epoch 38/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3376 - acc: 0.8967 - val_loss: 0.3241 - val_acc: 0.9035
Learning rate:  0.0001
Epoch 39/120
134/133 [==============================] - 32s 242ms/step - loss: 0.3302 - acc: 0.8990 - val_loss: 0.3438 - val_acc: 0.8978
Learning rate:  0.0001
Epoch 40/120
134/133 [==============================] - 32s 238ms/step - loss: 0.3348 - acc: 0.9014 - val_loss: 0.3357 - val_acc: 0.9025
Learning rate:  0.0001
Epoch 41/120
134/133 [==============================] - 32s 242ms/step - loss: 0.3349 - acc: 0.8962 - val_loss: 0.3448 - val_acc: 0.9044
Learning rate:  1e-05
Epoch 42/120
134/133 [==============================] - 32s 235ms/step - loss: 0.3252 - acc: 0.9028 - val_loss: 0.3270 - val_acc: 0.9091
Learning rate:  1e-05
Epoch 43/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3283 - acc: 0.8972 - val_loss: 0.3243 - val_acc: 0.9091
Learning rate:  1e-05
Epoch 44/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3311 - acc: 0.8955 - val_loss: 0.3236 - val_acc: 0.9072
Learning rate:  1e-05
Epoch 45/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3201 - acc: 0.8979 - val_loss: 0.3233 - val_acc: 0.9072
Learning rate:  1e-05
Epoch 46/120
134/133 [==============================] - 32s 238ms/step - loss: 0.3209 - acc: 0.9027 - val_loss: 0.3213 - val_acc: 0.9072
Learning rate:  1e-05
Epoch 47/120
134/133 [==============================] - 31s 235ms/step - loss: 0.3170 - acc: 0.9020 - val_loss: 0.3259 - val_acc: 0.9044
Learning rate:  1e-05
Epoch 48/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3261 - acc: 0.9002 - val_loss: 0.3269 - val_acc: 0.9063
Learning rate:  1e-05
Epoch 49/120
134/133 [==============================] - 31s 234ms/step - loss: 0.3261 - acc: 0.8995 - val_loss: 0.3235 - val_acc: 0.9044
Learning rate:  1e-05
Epoch 50/120
134/133 [==============================] - 33s 246ms/step - loss: 0.3197 - acc: 0.9006 - val_loss: 0.3266 - val_acc: 0.9072
Learning rate:  1e-05
Epoch 51/120
134/133 [==============================] - 33s 243ms/step - loss: 0.3103 - acc: 0.9053 - val_loss: 0.3278 - val_acc: 0.9025
Learning rate:  1e-06
Epoch 52/120
134/133 [==============================] - 32s 236ms/step - loss: 0.3189 - acc: 0.9053 - val_loss: 0.3274 - val_acc: 0.9035
Learning rate:  1e-06
Epoch 53/120
134/133 [==============================] - 31s 234ms/step - loss: 0.3183 - acc: 0.9065 - val_loss: 0.3257 - val_acc: 0.9035
Learning rate:  1e-06
Epoch 54/120
134/133 [==============================] - 32s 240ms/step - loss: 0.3108 - acc: 0.9070 - val_loss: 0.3258 - val_acc: 0.9053
Learning rate:  1e-06
Epoch 55/120
134/133 [==============================] - 34s 250ms/step - loss: 0.3180 - acc: 0.9018 - val_loss: 0.3244 - val_acc: 0.9035
Learning rate:  1e-06
Epoch 56/120
134/133 [==============================] - 32s 236ms/step - loss: 0.3199 - acc: 0.9016 - val_loss: 0.3256 - val_acc: 0.9044
Learning rate:  1e-06
Epoch 57/120
134/133 [==============================] - 31s 234ms/step - loss: 0.3216 - acc: 0.9028 - val_loss: 0.3267 - val_acc: 0.9025
Learning rate:  1e-06
Epoch 58/120
134/133 [==============================] - 32s 236ms/step - loss: 0.3254 - acc: 0.9004 - val_loss: 0.3279 - val_acc: 0.9035
Learning rate:  1e-06
Epoch 59/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3124 - acc: 0.9051 - val_loss: 0.3252 - val_acc: 0.9044
Learning rate:  1e-06
Epoch 60/120
134/133 [==============================] - 32s 242ms/step - loss: 0.3265 - acc: 0.8993 - val_loss: 0.3269 - val_acc: 0.9035
Learning rate:  1e-06
Epoch 61/120
134/133 [==============================] - 32s 240ms/step - loss: 0.3125 - acc: 0.9020 - val_loss: 0.3271 - val_acc: 0.9035
Learning rate:  1e-06
Epoch 62/120
134/133 [==============================] - 32s 236ms/step - loss: 0.3156 - acc: 0.9042 - val_loss: 0.3270 - val_acc: 0.9035
Learning rate:  1e-06
Epoch 63/120
134/133 [==============================] - 32s 238ms/step - loss: 0.3160 - acc: 0.9011 - val_loss: 0.3262 - val_acc: 0.9025
Learning rate:  1e-06
Epoch 64/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3179 - acc: 0.9039 - val_loss: 0.3265 - val_acc: 0.9025
Learning rate:  1e-06
Epoch 65/120
134/133 [==============================] - 32s 235ms/step - loss: 0.3204 - acc: 0.9018 - val_loss: 0.3265 - val_acc: 0.9035
Learning rate:  1e-06
Epoch 66/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3245 - acc: 0.9013 - val_loss: 0.3262 - val_acc: 0.9044
Learning rate:  1e-06
Epoch 67/120
134/133 [==============================] - 32s 239ms/step - loss: 0.3217 - acc: 0.8962 - val_loss: 0.3281 - val_acc: 0.9044
Learning rate:  1e-06
Epoch 68/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3121 - acc: 0.9079 - val_loss: 0.3276 - val_acc: 0.9044
Learning rate:  1e-06
Epoch 69/120
134/133 [==============================] - 32s 235ms/step - loss: 0.3174 - acc: 0.9049 - val_loss: 0.3273 - val_acc: 0.9035
Learning rate:  1e-06
Epoch 70/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3210 - acc: 0.9044 - val_loss: 0.3264 - val_acc: 0.9053
Learning rate:  1e-06
Epoch 71/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3212 - acc: 0.8953 - val_loss: 0.3264 - val_acc: 0.9044
Learning rate:  5e-07
Epoch 72/120
134/133 [==============================] - 31s 232ms/step - loss: 0.3279 - acc: 0.8955 - val_loss: 0.3270 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 73/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3194 - acc: 0.9032 - val_loss: 0.3281 - val_acc: 0.9044
Learning rate:  5e-07
Epoch 74/120
134/133 [==============================] - 31s 232ms/step - loss: 0.3151 - acc: 0.9051 - val_loss: 0.3276 - val_acc: 0.9044
Learning rate:  5e-07
Epoch 75/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3280 - acc: 0.8946 - val_loss: 0.3267 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 76/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3268 - acc: 0.8962 - val_loss: 0.3277 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 77/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3221 - acc: 0.8976 - val_loss: 0.3273 - val_acc: 0.9044
Learning rate:  5e-07
Epoch 78/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3194 - acc: 0.9044 - val_loss: 0.3274 - val_acc: 0.9044
Learning rate:  5e-07
Epoch 79/120
134/133 [==============================] - 32s 235ms/step - loss: 0.3180 - acc: 0.8986 - val_loss: 0.3271 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 80/120
134/133 [==============================] - 32s 236ms/step - loss: 0.3116 - acc: 0.9074 - val_loss: 0.3267 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 81/120
134/133 [==============================] - 31s 235ms/step - loss: 0.3156 - acc: 0.9013 - val_loss: 0.3266 - val_acc: 0.9053
Learning rate:  5e-07
Epoch 82/120
134/133 [==============================] - 31s 235ms/step - loss: 0.3110 - acc: 0.9046 - val_loss: 0.3259 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 83/120
134/133 [==============================] - 31s 232ms/step - loss: 0.3109 - acc: 0.9060 - val_loss: 0.3270 - val_acc: 0.9025
Learning rate:  5e-07
Epoch 84/120
134/133 [==============================] - 32s 236ms/step - loss: 0.3264 - acc: 0.9004 - val_loss: 0.3272 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 85/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3252 - acc: 0.9000 - val_loss: 0.3254 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 86/120
134/133 [==============================] - 31s 232ms/step - loss: 0.3236 - acc: 0.8969 - val_loss: 0.3265 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 87/120
134/133 [==============================] - 31s 232ms/step - loss: 0.3169 - acc: 0.8976 - val_loss: 0.3261 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 88/120
134/133 [==============================] - 31s 232ms/step - loss: 0.3108 - acc: 0.9055 - val_loss: 0.3261 - val_acc: 0.9044
Learning rate:  5e-07
Epoch 89/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3111 - acc: 0.9070 - val_loss: 0.3265 - val_acc: 0.9044
Learning rate:  5e-07
Epoch 90/120
134/133 [==============================] - 32s 239ms/step - loss: 0.3122 - acc: 0.9007 - val_loss: 0.3258 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 91/120
134/133 [==============================] - 31s 232ms/step - loss: 0.3149 - acc: 0.9044 - val_loss: 0.3264 - val_acc: 0.9016
Learning rate:  5e-07
Epoch 92/120
134/133 [==============================] - 31s 232ms/step - loss: 0.3173 - acc: 0.9014 - val_loss: 0.3266 - val_acc: 0.9025
Learning rate:  5e-07
Epoch 93/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3284 - acc: 0.8960 - val_loss: 0.3262 - val_acc: 0.9044
Learning rate:  5e-07
Epoch 94/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3210 - acc: 0.9004 - val_loss: 0.3268 - val_acc: 0.9025
Learning rate:  5e-07
Epoch 95/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3175 - acc: 0.9039 - val_loss: 0.3260 - val_acc: 0.9044
Learning rate:  5e-07
Epoch 96/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3176 - acc: 0.9051 - val_loss: 0.3276 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 97/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3184 - acc: 0.9037 - val_loss: 0.3266 - val_acc: 0.9016
Learning rate:  5e-07
Epoch 98/120
134/133 [==============================] - 32s 236ms/step - loss: 0.3090 - acc: 0.9053 - val_loss: 0.3259 - val_acc: 0.9025
Learning rate:  5e-07
Epoch 99/120
134/133 [==============================] - 32s 240ms/step - loss: 0.3067 - acc: 0.9055 - val_loss: 0.3257 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 100/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3295 - acc: 0.8990 - val_loss: 0.3262 - val_acc: 0.9016
Learning rate:  5e-07
Epoch 101/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3191 - acc: 0.9049 - val_loss: 0.3260 - val_acc: 0.9016
Learning rate:  5e-07
Epoch 102/120
134/133 [==============================] - 32s 241ms/step - loss: 0.3193 - acc: 0.9023 - val_loss: 0.3259 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 103/120
134/133 [==============================] - 31s 234ms/step - loss: 0.3198 - acc: 0.9004 - val_loss: 0.3258 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 104/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3115 - acc: 0.9060 - val_loss: 0.3257 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 105/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3118 - acc: 0.9023 - val_loss: 0.3261 - val_acc: 0.9025
Learning rate:  5e-07
Epoch 106/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3212 - acc: 0.8983 - val_loss: 0.3266 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 107/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3213 - acc: 0.9021 - val_loss: 0.3265 - val_acc: 0.9025
Learning rate:  5e-07
Epoch 108/120
134/133 [==============================] - 32s 235ms/step - loss: 0.3241 - acc: 0.8997 - val_loss: 0.3266 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 109/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3185 - acc: 0.9058 - val_loss: 0.3269 - val_acc: 0.9007
Learning rate:  5e-07
Epoch 110/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3195 - acc: 0.9042 - val_loss: 0.3271 - val_acc: 0.9025
Learning rate:  5e-07
Epoch 111/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3223 - acc: 0.8972 - val_loss: 0.3267 - val_acc: 0.9007
Learning rate:  5e-07
Epoch 112/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3178 - acc: 0.9018 - val_loss: 0.3270 - val_acc: 0.9025
Learning rate:  5e-07
Epoch 113/120
134/133 [==============================] - 32s 237ms/step - loss: 0.3180 - acc: 0.9018 - val_loss: 0.3249 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 114/120
134/133 [==============================] - 32s 235ms/step - loss: 0.3204 - acc: 0.9021 - val_loss: 0.3258 - val_acc: 0.9044
Learning rate:  5e-07
Epoch 115/120
134/133 [==============================] - 32s 240ms/step - loss: 0.3191 - acc: 0.8972 - val_loss: 0.3261 - val_acc: 0.9025
Learning rate:  5e-07
Epoch 116/120
134/133 [==============================] - 32s 236ms/step - loss: 0.3252 - acc: 0.8953 - val_loss: 0.3248 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 117/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3085 - acc: 0.9016 - val_loss: 0.3269 - val_acc: 0.9035
Learning rate:  5e-07
Epoch 118/120
134/133 [==============================] - 31s 233ms/step - loss: 0.3073 - acc: 0.9060 - val_loss: 0.3268 - val_acc: 0.9044
Learning rate:  5e-07
Epoch 119/120
134/133 [==============================] - 32s 236ms/step - loss: 0.3126 - acc: 0.9055 - val_loss: 0.3265 - val_acc: 0.9044
Learning rate:  5e-07
Epoch 120/120
134/133 [==============================] - 32s 236ms/step - loss: 0.3101 - acc: 0.9058 - val_loss: 0.3257 - val_acc: 0.9035
Out[12]:
<tensorflow.python.keras.callbacks.History at 0x22288fa1c18>
In [13]:
## Now the training is complete, we get
# another object to load the weights
# compile it, so that we can do 
# final evaluation on it
modelGo.load_weights(filepath)
modelGo.compile(loss='categorical_crossentropy', 
                optimizer='adam', 
                metrics=['accuracy'])
In [19]:
# Make classification on the test dataset
predicts    = modelGo.predict(tsDat)

# Prepare the classification output
# for the classification report
predout     = np.argmax(predicts,axis=1)
testout     = np.argmax(tsLbl,axis=1)
labelname   = ['non-flower', 'flower']
                                            # the labels for the classfication report


testScores  = metrics.accuracy_score(testout,predout)
confusion   = metrics.confusion_matrix(testout,predout)


print("Best accuracy (on testing dataset): %.2f%%" % (testScores*100))
print(metrics.classification_report(testout,predout,target_names=labelname,digits=4))
print(confusion)
Best accuracy (on testing dataset): 90.91%
              precision    recall  f1-score   support

  non-flower     0.8758    0.9163    0.8956       454
      flower     0.9358    0.9038    0.9195       613

    accuracy                         0.9091      1067
   macro avg     0.9058    0.9100    0.9075      1067
weighted avg     0.9103    0.9091    0.9093      1067

[[416  38]
 [ 59 554]]
In [15]:
import pandas as pd

records     = pd.read_csv(modelname +'.csv')
plt.figure()
plt.subplot(211)
plt.plot(records['val_loss'])
plt.plot(records['loss'])
plt.yticks([0, 0.20, 0.30, 0.4, 0.5])
plt.title('Loss value',fontsize=12)

ax          = plt.gca()
ax.set_xticklabels([])



plt.subplot(212)
plt.plot(records['val_acc'])
plt.plot(records['acc'])
plt.yticks([0.7, 0.8, 0.9, 1.0])
plt.title('Accuracy',fontsize=12)
plt.show()
In [16]:
wrong_ans_index = []

for i in range(len(predout)):
    if predout[i] != testout[i]:
        wrong_ans_index.append(i)
In [17]:
wrong_ans_index = list(set(wrong_ans_index))
In [18]:
# Randomly show X examples of that was wrong

dataset = tsDatOrg #flowers #fungus #rocks

for index in wrong_ans_index:
    #index = wrong_ans_index[random.randint(0, len(wrong_ans_index)-1)]
    print("Showing %s index image" %(index))
    print("Predicted as %s but is actually %s" %(predout[index], testout[index]))
    imgplot = plt.imshow(data[index])
    plt.show()
Showing 512 index image
Predicted as 0 but is actually 1
Showing 522 index image
Predicted as 0 but is actually 1
Showing 11 index image
Predicted as 0 but is actually 1
Showing 12 index image
Predicted as 0 but is actually 1
Showing 1047 index image
Predicted as 1 but is actually 0
Showing 1049 index image
Predicted as 1 but is actually 0
Showing 26 index image
Predicted as 0 but is actually 1
Showing 27 index image
Predicted as 0 but is actually 1
Showing 539 index image
Predicted as 0 but is actually 1
Showing 540 index image
Predicted as 0 but is actually 1
Showing 1054 index image
Predicted as 1 but is actually 0
Showing 1056 index image
Predicted as 1 but is actually 0
Showing 545 index image
Predicted as 0 but is actually 1
Showing 37 index image
Predicted as 0 but is actually 1
Showing 551 index image
Predicted as 0 but is actually 1
Showing 1064 index image
Predicted as 1 but is actually 0
Showing 553 index image
Predicted as 0 but is actually 1
Showing 72 index image
Predicted as 0 but is actually 1
Showing 89 index image
Predicted as 0 but is actually 1
Showing 92 index image
Predicted as 0 but is actually 1
Showing 605 index image
Predicted as 0 but is actually 1
Showing 615 index image
Predicted as 1 but is actually 0
Showing 616 index image
Predicted as 1 but is actually 0
Showing 110 index image
Predicted as 0 but is actually 1
Showing 624 index image
Predicted as 1 but is actually 0
Showing 113 index image
Predicted as 0 but is actually 1
Showing 131 index image
Predicted as 0 but is actually 1
Showing 133 index image
Predicted as 0 but is actually 1
Showing 645 index image
Predicted as 1 but is actually 0
Showing 137 index image
Predicted as 0 but is actually 1
Showing 650 index image
Predicted as 1 but is actually 0
Showing 141 index image
Predicted as 0 but is actually 1
Showing 147 index image
Predicted as 0 but is actually 1
Showing 148 index image
Predicted as 0 but is actually 1
Showing 164 index image
Predicted as 0 but is actually 1
Showing 169 index image
Predicted as 0 but is actually 1
Showing 176 index image
Predicted as 0 but is actually 1
Showing 695 index image
Predicted as 1 but is actually 0
Showing 204 index image
Predicted as 0 but is actually 1
Showing 205 index image
Predicted as 0 but is actually 1
Showing 220 index image
Predicted as 0 but is actually 1
Showing 733 index image
Predicted as 1 but is actually 0
Showing 228 index image
Predicted as 0 but is actually 1
Showing 743 index image
Predicted as 1 but is actually 0
Showing 233 index image
Predicted as 0 but is actually 1
Showing 752 index image
Predicted as 1 but is actually 0
Showing 250 index image
Predicted as 0 but is actually 1
Showing 253 index image
Predicted as 0 but is actually 1
Showing 259 index image
Predicted as 0 but is actually 1
Showing 269 index image
Predicted as 0 but is actually 1
Showing 790 index image
Predicted as 1 but is actually 0
Showing 279 index image
Predicted as 0 but is actually 1
Showing 792 index image
Predicted as 1 but is actually 0
Showing 794 index image
Predicted as 1 but is actually 0
Showing 288 index image
Predicted as 0 but is actually 1
Showing 805 index image
Predicted as 1 but is actually 0
Showing 806 index image
Predicted as 1 but is actually 0
Showing 311 index image
Predicted as 0 but is actually 1
Showing 830 index image
Predicted as 1 but is actually 0
Showing 329 index image
Predicted as 0 but is actually 1
Showing 336 index image
Predicted as 0 but is actually 1
Showing 852 index image
Predicted as 1 but is actually 0
Showing 347 index image
Predicted as 0 but is actually 1
Showing 349 index image
Predicted as 0 but is actually 1
Showing 877 index image
Predicted as 1 but is actually 0
Showing 365 index image
Predicted as 0 but is actually 1
Showing 366 index image
Predicted as 0 but is actually 1
Showing 367 index image
Predicted as 0 but is actually 1
Showing 371 index image
Predicted as 0 but is actually 1
Showing 377 index image
Predicted as 0 but is actually 1
Showing 893 index image
Predicted as 1 but is actually 0
Showing 382 index image
Predicted as 0 but is actually 1
Showing 384 index image
Predicted as 0 but is actually 1
Showing 904 index image
Predicted as 1 but is actually 0
Showing 914 index image
Predicted as 1 but is actually 0
Showing 919 index image
Predicted as 1 but is actually 0
Showing 921 index image
Predicted as 1 but is actually 0
Showing 420 index image
Predicted as 0 but is actually 1
Showing 942 index image
Predicted as 1 but is actually 0
Showing 435 index image
Predicted as 0 but is actually 1
Showing 947 index image
Predicted as 1 but is actually 0
Showing 955 index image
Predicted as 1 but is actually 0
Showing 958 index image
Predicted as 1 but is actually 0
Showing 964 index image
Predicted as 1 but is actually 0
Showing 974 index image
Predicted as 1 but is actually 0
Showing 975 index image
Predicted as 1 but is actually 0
Showing 469 index image
Predicted as 0 but is actually 1
Showing 982 index image
Predicted as 1 but is actually 0
Showing 471 index image
Predicted as 0 but is actually 1
Showing 985 index image
Predicted as 1 but is actually 0
Showing 474 index image
Predicted as 0 but is actually 1
Showing 487 index image
Predicted as 0 but is actually 1
Showing 498 index image
Predicted as 0 but is actually 1
Showing 1015 index image
Predicted as 1 but is actually 0
Showing 505 index image
Predicted as 0 but is actually 1
Showing 1022 index image
Predicted as 1 but is actually 0
Showing 511 index image
Predicted as 0 but is actually 1
In [ ]: